//
//  ZhougongView.m
//  LiuHeBao
//
//  Created by zr on 2018/8/23.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ZhougongView.h"

@interface ZhougongView ()<UITextFieldDelegate>

@property (nonatomic, weak)UITextField * searchTextfiled;
@property (nonatomic, weak)UILabel * titleLabel;
//@property (nonatomic, weak)UITextView * textView;
@property(nonatomic,weak)UIWebView *webView;

@end

@implementation ZhougongView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = WHITE_COLOR;
    self.userInteractionEnabled = YES;
    
    UITextField * searchTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(62 * SCREENH6, 10 * SCREENH6, 251 * SCREENH6, 25 * SCREENH6)];
    
    searchTextfiled.borderStyle = UITextBorderStyleRoundedRect;
    searchTextfiled.backgroundColor = [UIColor whiteColor];
    //设置光标颜色
    searchTextfiled.tintColor = BLACK_COLOR;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xbebebe);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    searchTextfiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索梦境关键词" attributes:attrs];
    searchTextfiled.textColor = [UIColor blackColor];
    searchTextfiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 16 * SCREENH6, 17.5 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image.image = ImageNamed(@"hg");
    [rightView addSubview:image];
    searchTextfiled.leftView = rightView;
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.returnKeyType = UIReturnKeySearch;
    searchTextfiled.delegate = self;
    
    self.searchTextfiled = searchTextfiled;
    
    [self addSubview:self.searchTextfiled];
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = MAINCOLOR;
    titleLabel.font = [UIFont systemFontOfSize:20 * SCREENH6];
    titleLabel.text = @"梦境分析";
    
    self.titleLabel = titleLabel;
    
    [self addSubview:self.titleLabel];
    
    
//    UITextView *textView =[[UITextView alloc] init];
//    textView.backgroundColor= WHITE_COLOR;
//    textView.text = @"";
//    textView.textColor = [UIColor blackColor];
//    textView.editable = NO;
//    textView.font = [UIFont systemFontOfSize:15 * SCREENH6];
//
//    self.textView = textView;
//
//    [self addSubview:self.textView];
    
    UIWebView *webView = [[UIWebView alloc] init];
    webView.scalesPageToFit = YES;
    //    webView.delegate = self;
    webView.backgroundColor = WHITE_COLOR;
    //    NSURL *urlPath = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
    //
    //    [webView loadHTMLString:self.contentStr baseURL:urlPath];
    
    self.webView = webView;
    
    [self addSubview:self.webView];

}


-(void)layoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.searchTextfiled.mas_bottom).offset(10 * SCREENH6);
    }];
    
//    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
//        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
//        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(7 * SCREENH6);
//        make.bottom.mas_equalTo(self);
//    }];
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(7 * SCREENH6);
        make.bottom.mas_equalTo(self);
    }];
}


//实现UITextField代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
//    NSLog(@"=====%@",textField);
    NSString * url = [NSString stringWithFormat:@"/index/Liuhetreasure/getArticle?type=4&title=%@",textField.text];
    NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
    NSString *encodeUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
    
    [[NetworkTool sharedTools] GET:encodeUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"====%@",responseObject);
        
        NSArray * data = responseObject[@"data"];
        
        NSDictionary * dict = data.firstObject;
        
//        self.textView.text = dict[@"content"];
        NSURL *urlPath = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
        
        [self.webView loadHTMLString:dict[@"content"] baseURL:urlPath];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
    return YES;
}



@end
